<?php
/**  
 * 产品中心 
 * @access public|private|protected
 * @param string xxx
 * @return void|int|string|boolean|array        comment 
 * @author      ZhangLiLi <379417738@qq.com>
 * @version	  v1.0.0 
 * @copyright  2017-1-18 上午9:53:50
*/
namespace Admin\Controller;
use Admin\Controller\AdminController;
class ProductController extends AdminController{
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	/**
	 * 产品中心列表显示
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-18 上午9:54:07
	 */
	public function index(){
		$where['gy_position.status']=array("NEQ","-1");
		$where['gy_ptype.status']=array("NEQ","-1");
		$where['gy_position.position_id']=array('in',array(3,4));
		$count=M("Product")
				->field("gy_product.*,gy_position.name as pname,gy_ptype.tname")
				->join("gy_position ON gy_product.position_id=gy_position.position_id")
				->join("gy_ptype ON gy_product.tid=gy_ptype.tid")
				->where($where)
				->count();
		$Page   = new \Think\Page($count,10);
		$show   = $Page->show();
		$list=M("Product")
			->field("gy_product.*,gy_position.name as pname,gy_ptype.tname")
			->join("gy_position ON gy_product.position_id=gy_position.position_id")
			->join("gy_ptype ON gy_product.tid=gy_ptype.tid")
			->where($where)
			->order("add_time desc")
			->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 产品中心添加或修改页显示
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-18 上午9:54:36
	 */
	public function insert(){
		$where['id']=I('id',"");
		if(!empty($where['id'])){
			$but="修改";
			$product = M('Product') -> where($where) -> find();
			$plist=M("Ptype")->where(array("tid"=>$product["tid"]))->find();
			if($plist["pid"]==0){
				$re=M("Ptype")->where(array("tname"=>$plist["tname"]))->find();
			}else{
				$re=M("Ptype")->where(array("tid"=>$plist["tid"]))->find();
			}
			
			$this -> assign('re',$re);
// 			die;
			$this -> assign('product',$product);
			$this -> assign('plist',$pist);
			
		}else{
			$but="添加";
		}
		//分级类别的显示
		//分类显示
		//查找一级分类
		$where["pid"]=0;
		$where["status"]=array("NEQ","-1");
		$list=M("Ptype")->where($where)->select();
		//以一级分类为基础，形成遍历
		foreach ($list as $k=>$v){
			//加入二级分类，以一级id为查询条件
			$where1["pid"]=$v["tid"];
			$where1["status"]=array("NEQ","-1");
			$list[$k]["children"]=M("Ptype")->where($where1)->select();
		}
		$this->assign("list",$list);
		//显示位置
		
		$where2["status"]=array("NEQ","-1");
		$where2["position_id"]=array("in",array(3,4));
		$position=M("Position")->where($where2)->order("position_id asc")->select();
		$this->assign("position",$position);
		$this->assign("but",$but);
		$this->display();
	}
	/**
	 * 产品中心修改或添加
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-18 上午9:54:54
	 */
	public function add(){
		$where["id"]=I("id","");
		$data["title"]=I("title","");
		$data["tid"]=I("tid",0);
		$data["position_id"]=I("position_id","");
		$data["content"]=htmlspecialchars_decode($_POST["content"]);
		$data["keywords"]=I("keywords","");
		$data["add_time"]=time();
		if($_FILES["Filedata"]["name"]){
			$file=$_FILES["Filedata"];
			$data['pic']=$this->upload($file);
			$url=M('Banner')->where($where)->find();
			unlink($_SERVER['DOCUMENT_ROOT'].$url['pic1']);
		}else{
			$data['pic']=!empty($_POST['pic_1'])?I('pic_1'):null;
		}
		if(empty($data['pic'])){
			$this->error("请上传图片");
		}
		if(empty($where["id"])){
			//添加
			$data["status"]=1;
			if(empty($data["title"])){
				$this->error("请填写标题");
			}
			if(empty($data["position_id"])){
				$this->error("请选择显示位置");
			}
			if(empty($data["content"])){
				$this->error("请填写产品详情");
			}
			$re=M("Product")->add($data);
// 			dump($re);die;
			$su_str='添加成功';
			$er_str='添加失败';
		}else{
			//修改
			if(empty($data["title"])){
				$this->error("请填写标题");
			}
			if(empty($data["tid"])){
				$this->error("请选择分类");
			}
			if(empty($data["position_id"])){
				$this->error("请选择显示位置");
			}
			if(empty($data["content"])){
				$this->error("请填写产品详情");
			}
			$re=M("Product")->where($where)->save($data);
			$su_str='修改成功';
			$er_str='修改失败';
		}
		if($re){
			$this -> success($su_str,U('Product/index'));
		}else{
			$this -> error($er_str);
		}
	}
	/**
	 * 修改状态
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-18 上午10:23:19
	 */
	public function open(){
    	$where['id']=I('id')?I('id'):null;
    	$list=M('Product')->where($where)->find();
    	if($list['status']==2){
    		$list=M('Product')->where($where)->setField('status','1');
    		$this->success('状态修改成功');
    	}
    	if($list['status']==1){
    		$list=M('Product')->where($where)->setField('status','2');
    		$this->success('状态修改成功');
    	}
    }
    /**
     * 删除
     * @author      ZhangLiLi <379417738@qq.com>
     * @version	  v1.0.0 
     * @copyright  2017-1-18 上午10:24:15
     */
	public function delete(){
    	$where['id']=I("id","");
		if(!empty($where['id'])){
			$list=M('Product')->where($where)->find();
			if($list["status"]==2){
				$res=M('Product')->where($where)->setField('status','-1');
				if($res){
					$this->success('删除成功',U('Product/index'));
				}else{
					$this->error('删除失败');
				}
			}else{
				$this->error("显示状态下不能删除");
			}
		} 
    }
}